home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / packet / terminal / tpk_182 / servinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  6.6 KB  |  314 lines

  1. /*
  2.  
  3. SERVINFO.C
  4.  
  5. Ce programme est un serveur pour TPK. Il repond aux messages dont le
  6. titre contient la commande /INFO ou /INFO nnn
  7.  
  8. Il retourne en reponse le fichier INFO.000 dans le premier cas et le
  9. fichier INFO.nnn dans le second cas.
  10.  
  11. La reponse est ecrite dans le fichier d'import de messages de TPK dont
  12. le nom implicite est MAIL.IN. Ce nom peut etre change dans le fichier de
  13. configuration SERVINFO.CFG et dans TPK par la commande FORward IMport
  14.  
  15. Commandes du fichier SERVINFO.CFG:
  16.  
  17. PATH
  18. Chemin des fichiers INFO.xxx
  19. Ex: PATH C:\TPK\INFO
  20. SERVINFO ira lire les fichiers C:\TPK\INFO\INFO.xxx (xxx=000 a 999)
  21.  
  22. TPK
  23. Repertoire de TPK (facultatif car arrive en parametre)
  24. Ex: TPK C:\TPK
  25.  
  26. MYCALL
  27. Indicatif utilise (facultatif car arrive en parametre)
  28. Ex: MYCALL F1EBN
  29.  
  30. MAIL
  31. Nom du fichier d'import de messages (MAIL.IN implicite)
  32. Ex: MAIL IMPORT.MSG
  33.  
  34. FILE
  35. Nom des fichiers (INFO implicite)
  36. Ex: FILE TEXT_INF
  37. Dans ce cas SERVINFO ira chercher des fichiers TEXT_INF.000 a 999
  38.  
  39. */
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <ctype.h>
  45.  
  46. void ReadCfg(void);
  47. void ReadWriteInfo(void);
  48. int ReadDest (char *cmd);
  49. int ReadFromBbs (char *cmd);
  50.  
  51. char str[255];
  52. char line[256];
  53. char N_Mycall[7]="\0";
  54. char Path_Tpk[129];
  55. char Path_Info[129];
  56. char FiMail[13]="MAIL.IN";
  57. char FiInfo[9]="INFO";
  58. char FiMsg[129];
  59. char Typ[2],To[7],From[7],Abbs[41],Bid[13],BidOrg[13];
  60. char Fbbs[41],FbbsCall[7];
  61. char Subject[81];
  62.  
  63. void main(int argc, char *argv[])
  64. {
  65. FILE *fichin,*fichout;
  66. char *s;
  67. int WaitFrom,Nbcar;
  68.  
  69. char *ptr;
  70.  
  71. if (argc) strncpy(FiMsg, argv[1], 128);
  72. if (argc>1) strncpy(Path_Tpk, argv[2], 128);
  73. if (argc=3) strncpy(N_Mycall, strupr(argv[3]), 6);
  74.  
  75. ReadCfg();
  76.  
  77. if ((fichin=fopen(FiMsg, "r")) != NULL)
  78.     {
  79.     if (fgets(line, 255, fichin) != NULL)
  80.         {
  81.         /* Lecture du destinataire */
  82.         ReadDest (line);
  83.         /* Message pour moi ? */
  84.         if (strcmp(To, N_Mycall) == 0)
  85.             {
  86.             if (fgets(Subject, 80, fichin) != NULL)
  87.                 {
  88.                 /* Est-ce que le sujet est /INFO ? */
  89.                 if ((strstr(strupr(Subject), "/INFO")) == Subject)
  90.                     {
  91.                     /* Sujet = /INFO donc c'est ok */
  92.                     WaitFrom=1;
  93.                     Nbcar=0;
  94.                     /* Recherche de la BBS d'origine */
  95.                     while (fgets(line, 255, fichin) != NULL)
  96.                         {
  97.                         if (WaitFrom)
  98.                             {
  99.                             if ((s=strstr(line, "R:")) != NULL)
  100.                                 {
  101.                                 ReadFromBbs (s);
  102.                                 }
  103.                             else
  104.                                 {
  105.                                 WaitFrom--;
  106.                                 break;
  107.                                 }
  108.                             }
  109.                         }
  110.                     /* Lecture du fichier et creation du message de
  111.                     reponse */
  112.                     ReadWriteInfo();
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     fclose(fichin);
  118.     }
  119. }
  120.  
  121. /* Lecture de la configuration */
  122.  
  123. void ReadCfg(void)
  124. {
  125. int i;
  126. char cmd[81];
  127. char par1[81];
  128. char par2[81];
  129. char *s;
  130. FILE *fichin;
  131.  
  132. if (s=strcpy(str, Path_Tpk),s=strcat(str, "SERVINFO.CFG"),
  133.     (fichin=fopen(str, "r")) != NULL)
  134.     {
  135.     while (fgets(str, 80, fichin) !=NULL)
  136.         {
  137.         i=sscanf(str, "%s", cmd);
  138.         if (i){
  139.             i=sscanf(str, "%s %s %s", cmd,par1,par2);
  140.             strcpy(cmd,strupr(cmd));
  141.             /* Chemin des fichiers INFO.xxx */
  142.             if (strcmp("PATH",cmd)==0)
  143.                 {
  144.                 strncpy(Path_Info, strupr(par1), 128);
  145.                 if ((*(Path_Info+strlen(Path_Info))) != '\\')
  146.                     strcat(Path_Info, "\\");
  147.                 }
  148.             /* Repertoire de TPK (facultatif car arrive en parametre) */
  149.             if (strcmp("TPK",cmd)==0)
  150.                 {
  151.                 strncpy(Path_Tpk, strupr(par1), 128);
  152.                 if ((*(Path_Tpk+strlen(Path_Tpk))) != '\\')
  153.                     strcat(Path_Tpk, "\\");
  154.                 }
  155.  
  156.             /* Indicatif utilise (facultatif car arrive en parametre) */
  157.             if (strcmp("MYCALL",cmd)==0) strncpy(N_Mycall, strupr(par1), 6);
  158.             /* Nom du fichier d'import de messages (MAIL.IN implicite)*/
  159.             if (strcmp("MAIL",cmd)==0) strncpy(FiMail, strupr(par1), 12);
  160.             /* Nom des fichiers (INFO implicite) */
  161.             if (strcmp("FILE",cmd)==0) strncpy(FiInfo, strupr(par1), 8);
  162.             }
  163.         }
  164.     fclose(fichin);
  165.     }
  166. }
  167.  
  168. /* Lecture des informations dans une ligne de commande SP ou SB */
  169.  
  170. int ReadDest (char *cmd)
  171. {
  172. char *s;
  173. char *ptr;
  174. int i;
  175.  
  176. if (strlen(cmd)>2)
  177.     {
  178.     if (toupper(*cmd)==83)
  179.         {
  180.         s=cmd;
  181.         s=strupr(s);
  182.         *Typ=*++s;
  183.         ptr=To;                 /* TO */
  184.         i=6;
  185.         for (++s;*s ;s++)
  186.             {
  187.             if (*s==64){        /* @BBS*/
  188.                 ptr=Abbs;
  189.                 *ptr=0;
  190.                 i=40;}
  191.             else if (*s==60){   /* <FROM */
  192.                 ptr=From;
  193.                 *ptr=0;
  194.                 i=6;}
  195.             else if (*s==36){   /* $BID */
  196.                 ptr=Bid;
  197.                 *ptr=0;
  198.                 i=12;}
  199.             else if (*s<33)
  200.                 continue;
  201.             else if (i)
  202.                 {
  203.                 if (*s>32)
  204.                     {
  205.                     *ptr++=*s;
  206.                     *ptr=0;
  207.                     i--;
  208.                     }
  209.                 }
  210.             }
  211.             return strlen(To);
  212.         }
  213.     }
  214. }
  215.  
  216. /* Recherche de la BBS d'origine du message dans une ligne R: */
  217.  
  218. int ReadFromBbs (char *cmd)
  219. {
  220. char *s;
  221. char *ptr;
  222. int i=0;
  223. for (s=cmd; *s; s++)
  224.     {
  225.     if (*s==64){
  226.         ptr=Fbbs;
  227.         *ptr=0;
  228.         i=40;
  229.         }
  230.     else if (*s==36){
  231.         ptr=BidOrg;
  232.         *ptr=0;
  233.         i=12;
  234.         }
  235.     else if (*s==58)
  236.         continue;
  237.     else if (*s==32)
  238.         i=0;
  239.     else if (i)
  240.         {
  241.         if (*s>32)
  242.             {
  243.             *ptr++=*s;
  244.             *ptr=0;
  245.             i--;
  246.             }
  247.         }
  248.     }
  249. return strlen(Fbbs);
  250. }
  251.  
  252. /* Lecture du fichier INFO et ecriture du message dans le fichier */
  253. /* d'import de TPK (MAIL.IN)                                      */
  254.  
  255. void ReadWriteInfo(void)
  256. {
  257. char str1[129];
  258. char str2[4];
  259. char *s,*st;
  260. int i;
  261. FILE *fichin,*fichout;
  262.  
  263. i=sscanf(Subject, "%s %s", str1, str1);
  264. if (i==1)
  265.     {
  266.     *(Subject+5)=' ';
  267.     strcat(Subject,"0\n");
  268.     }
  269. for (s=Subject+5; *s; s++)
  270.     {
  271.     for (st=str1, *st='\0'; *s ; s++)
  272.         {
  273.         if ( *s<':',*s>'/')
  274.             {
  275.             *st++=*s;
  276.             *st='\0';
  277.             }
  278.         else if (*s==32 | *s=='\n' )
  279.             {
  280.  
  281.             if (i=strlen(str1))
  282.                 {
  283.                 sprintf(str2, "%03d", atoi(str1));
  284.                 sprintf(str1, "%s%s.%s", Path_Info, FiInfo, str2);
  285.                 if (st=strcpy(str, Path_Tpk),st=strcat(str, FiMail),
  286.                     (fichout=fopen(str, "a")) != NULL)
  287.                     {
  288.                     if (strlen(Fbbs))
  289.                         fprintf(fichout, "SP %s @%s <%s\n", From, Fbbs, N_Mycall);
  290.                     else
  291.                         fprintf(fichout, "SP %s <%s\n", From, N_Mycall);
  292.                     fprintf(fichout, "Re:/INFO %s\n", str2);
  293.                     if ((fichin=fopen(str1, "r")) != NULL)
  294.                         {
  295.                         while (fgets(str, 80, fichin) !=NULL)
  296.                             {
  297.                             fputs(str, fichout);
  298.                             }
  299.                         fclose (fichin);
  300.                         }
  301.                     else
  302.                         {
  303.                         fprintf(fichout, "Pas d'info %s\n", str2);
  304.                         }
  305.                     fputs("/EX\n", fichout);
  306.                     fclose (fichout);
  307.                     }
  308.                 }
  309.             st=str1;
  310.             }
  311.         }
  312.     }
  313. }
  314.